﻿using System;
using System.Data;
using CardLogistics.Web.Components;
using System.Text;
using System.Web.UI.WebControls;

namespace CardLogistics.Web.MainPage.SystemMgr.SysBillRule
{
    public partial class ListInfo : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRpt(0);
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary>
        /// <param name="pageIndex"></param>
        private void BindRpt(int pageIndex)
        {
            int recordCount = 0;
            int pageCount = 0;
            DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("Sys_BillRule", "ID", " Sort ", AspNetPager1.PageSize, pageIndex, "", "*", out pageCount, out recordCount);
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);

        }
        /// <summary>
        /// 绑定下拉框
        /// </summary>
        public static void BindDLL(DropDownList ddl)
        {
            DataTable tblDatas = new DataTable("Datas");
            DataColumn dc = null;
            dc = tblDatas.Columns.Add("BillTest", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("BillValue", Type.GetType("System.String"));
            DataRow newRow;
            newRow = tblDatas.NewRow();
            newRow["BillTest"] = "---请选择---";
            newRow["BillValue"] = "";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["BillTest"] = "四位年";
            newRow["BillValue"] = "yyyy";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["BillTest"] = "两位年";
            newRow["BillValue"] = "yy";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["BillTest"] = "月";
            newRow["BillValue"] = "MM";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["BillTest"] = "两位编号";
            newRow["BillValue"] = "00";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["BillTest"] = "三位编号";
            newRow["BillValue"] = "000";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["BillTest"] = "四位编号";
            newRow["BillValue"] = "0000";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["BillTest"] = "字母";
            newRow["BillValue"] = "AA";//选择字母时，值从文本框中获取
            tblDatas.Rows.Add(newRow);

            ddl.DataSource = tblDatas;
            ddl.DataTextField = "BillTest";
            ddl.DataValueField = "BillValue";
            ddl.DataBind();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt(AspNetPager1.CurrentPageIndex);
        }
        /// <summary>
        /// 绑定项事件 设置下拉框选中项、设置文本框是可见性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //编号规则设置一
                DropDownList ddlNUMOne = e.Item.FindControl("ddl_NUMONE") as DropDownList;
                BindDLL(ddlNUMOne);//绑定下拉框数据源
                TextBox txt_NUMONE = e.Item.FindControl("txt_NUMONE") as TextBox;
                if (txt_NUMONE.Text.Trim().Length > 0)
                {
                    if (ddlNUMOne.Items.FindByValue(txt_NUMONE.Text.Trim()) == null)
                        ddlNUMOne.SelectedIndex = 7;//字母
                    else
                        ddlNUMOne.SelectedValue = txt_NUMONE.Text.Trim();
                }
                if (ddlNUMOne.SelectedIndex != 7)//字母
                    txt_NUMONE.Visible = false;
                //编号规则设置二
                DropDownList ddlNUMTWO = e.Item.FindControl("ddl_NUMTWO") as DropDownList;
                BindDLL(ddlNUMTWO);//绑定下拉框数据源
                TextBox txt_NUMTWO = e.Item.FindControl("txt_NUMTWO") as TextBox;
                if (txt_NUMTWO.Text.Trim().Length > 0)
                {

                    if (ddlNUMTWO.Items.FindByValue(txt_NUMTWO.Text.Trim()) == null)
                        ddlNUMTWO.SelectedIndex = 7;//字母
                    else
                        ddlNUMTWO.SelectedValue = txt_NUMTWO.Text.Trim();
                }
                if (ddlNUMTWO.SelectedIndex != 7)//字母
                    txt_NUMTWO.Visible = false;
                //编号规则设置三
                DropDownList ddlNUMTHREE = e.Item.FindControl("ddl_NUMTHREE") as DropDownList;
                BindDLL(ddlNUMTHREE);//绑定下拉框数据源
                TextBox txt_NUMTHREE = e.Item.FindControl("txt_NUMTHREE") as TextBox;
                if (txt_NUMTHREE.Text.Trim().Length > 0)
                {

                    if (ddlNUMTHREE.Items.FindByValue(txt_NUMTHREE.Text.Trim()) == null)
                        ddlNUMTHREE.SelectedIndex = 7;//字母
                    else
                        ddlNUMTHREE.SelectedValue = txt_NUMTHREE.Text.Trim();
                }
                if (ddlNUMTHREE.SelectedIndex != 7)//字母
                    txt_NUMTHREE.Visible = false;
                //编号规则设置四
                DropDownList ddlNUMFOUR = e.Item.FindControl("ddl_NUMFOUR") as DropDownList;
                BindDLL(ddlNUMFOUR);//绑定下拉框数据源
                TextBox txt_NUMFOUR = e.Item.FindControl("txt_NUMFOUR") as TextBox;
                if (txt_NUMFOUR.Text.Trim().Length > 0)
                {
                    if (ddlNUMFOUR.Items.FindByValue(txt_NUMFOUR.Text.Trim()) == null)
                        ddlNUMTHREE.SelectedIndex = 7;//字母
                    else
                        ddlNUMFOUR.SelectedValue = txt_NUMFOUR.Text.Trim();
                }
                if (ddlNUMFOUR.SelectedIndex != 7)//字母
                    txt_NUMFOUR.Visible = false;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptList_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            string strID = e.CommandArgument.ToString();
            Model.Sys.Sys_BillRule model = new Model.Sys.Sys_BillRule();
            if (e.CommandName == "ibEdit")  //编辑
            {
                Label BILLNAME = e.Item.FindControl("lb_BILLNAME") as Label;
                TextBox txt_NUMONE = e.Item.FindControl("txt_NUMONE") as TextBox;
                TextBox txt_NUMTWO = e.Item.FindControl("txt_NUMTWO") as TextBox;
                TextBox txt_NUMTHREE = e.Item.FindControl("txt_NUMTHREE") as TextBox;
                TextBox txt_NUMFOUR = e.Item.FindControl("txt_NUMFOUR") as TextBox;
                TextBox txt_SORT = e.Item.FindControl("txt_SORT") as TextBox;

                DropDownList ddlNUMOne = e.Item.FindControl("ddl_NUMONE") as DropDownList;
                DropDownList ddlNUMTWO = e.Item.FindControl("ddl_NUMTWO") as DropDownList;
                DropDownList ddlNUMTHREE = e.Item.FindControl("ddl_NUMTHREE") as DropDownList;
                DropDownList ddlNUMFOUR = e.Item.FindControl("ddl_NUMFOUR") as DropDownList;

                #region 封装Model
                model.ID = strID; //主键Id
                model.BILLNAME = BILLNAME.Text.Trim();//单据名称
                //获取规则一
                if (ddlNUMOne.SelectedItem.Text == "字母")
                {
                    model.NUMONE = txt_NUMONE.Text.Trim();
                }
                else
                {
                    model.NUMONE = ddlNUMOne.SelectedValue;
                }
                //获取规则二
                if (ddlNUMTWO.SelectedItem.Text == "字母")
                {
                    model.NUMTWO = txt_NUMTWO.Text.Trim();
                }
                else
                {
                    model.NUMTWO = ddlNUMTWO.SelectedValue;
                }
                //获取规则三
                if (ddlNUMTHREE.SelectedItem.Text == "字母")
                {
                    model.NUMTHREE = txt_NUMTHREE.Text.Trim();
                }
                else
                {
                    model.NUMTHREE = ddlNUMTHREE.SelectedValue;
                }
                //获取规则四
                if (ddlNUMFOUR.SelectedItem.Text == "字母")
                {
                    model.NUMFOUR = txt_NUMFOUR.Text.Trim();
                }
                else
                {
                    model.NUMFOUR = ddlNUMFOUR.SelectedValue;
                }
                model.SORT = int.Parse(txt_SORT.Text.Trim());//排序
                #endregion
                string result = new DAL.Sys.Sys_BillRuleData().Update(model);
                if (result.Split(',')[0] == "ok")
                {
                    string message = model.NUMONE + model.NUMTWO + model.NUMTHREE + model.NUMFOUR;
                    //记录日志
                    InsertLog("设置单号规则：" + model.BILLNAME + message);
                    ShowMessageBox("单号规则设置成功！" + model.BILLNAME + " 规则为：" + message.Replace("y","Y"));
                    BindRpt(0);
                }
                else
                {
                    ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
                }
            }
        }
        /// <summary>
        /// 设置文本框是否显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void setTextBoxVisiable(object sender, EventArgs e)
        {
            foreach (RepeaterItem rpi in rptList.Items)
            {
                //编号规则设置一
                DropDownList ddlNUMOne = rpi.FindControl("ddl_NUMONE") as DropDownList;
                TextBox txt_NUMONE = rpi.FindControl("txt_NUMONE") as TextBox;
                if (ddlNUMOne.SelectedIndex == 7)//字母
                {
                    txt_NUMONE.Visible = true;
                    if (txt_NUMONE.Text == "yyyy" || txt_NUMONE.Text == "yy" || txt_NUMONE.Text == "0000" || txt_NUMONE.Text == "00")
                        txt_NUMONE.Text = "";
                }
                //编号规则设置二
                DropDownList ddlNUMTWO = rpi.FindControl("ddl_NUMTWO") as DropDownList;
                TextBox txt_NUMTWO = rpi.FindControl("txt_NUMTWO") as TextBox;
                if (ddlNUMTWO.SelectedIndex == 7)//字母
                {
                    txt_NUMTWO.Visible = true;
                    if (txt_NUMTWO.Text == "yyyy" || txt_NUMTWO.Text == "yy" || txt_NUMTWO.Text == "0000" || txt_NUMTWO.Text == "00")
                        txt_NUMTWO.Text = "";
                }
                //编号规则设置三
                DropDownList ddlNUMTHREE = rpi.FindControl("ddl_NUMTHREE") as DropDownList;
                TextBox txt_NUMTHREE = rpi.FindControl("txt_NUMTHREE") as TextBox;
                if (ddlNUMTHREE.SelectedIndex == 7)//字母
                {
                    txt_NUMTHREE.Visible = true;
                    if (txt_NUMTHREE.Text == "yyyy" || txt_NUMTHREE.Text == "yy" || txt_NUMTHREE.Text == "0000" || txt_NUMTHREE.Text == "00")
                        txt_NUMTHREE.Text = "";
                }
                //编号规则设置四
                DropDownList ddlNUMFOUR = rpi.FindControl("ddl_NUMFOUR") as DropDownList;
                TextBox txt_NUMFOUR = rpi.FindControl("txt_NUMFOUR") as TextBox;
                if (ddlNUMFOUR.SelectedIndex == 7)//字母
                {
                    txt_NUMFOUR.Visible = true;
                    if (txt_NUMFOUR.Text == "yyyy" || txt_NUMFOUR.Text == "yy" || txt_NUMFOUR.Text == "0000" || txt_NUMFOUR.Text == "00")
                        txt_NUMFOUR.Text = "";
                }
            }
        }

    }
}